# src/service/collection_service.py
from src.db.collection_db import add_collection, remove_collection, get_user_collections
from src.db.user_db import get_user_by_id
from src.db.artwork_db import get_artwork_by_id
from src.utils.logger import get_logger

logger = get_logger(__name__)

def add_artwork_to_collection(user_id: int, artwork_id: int) -> tuple[bool, str]:
    """
    添加艺术品到收藏
    返回：(是否成功, 提示信息)
    """
    # 1. 校验用户身份
    user = get_user_by_id(user_id)
    if not user:
        logger.warning(f"用户不存在（user_id: {user_id}）")
        return False, "用户不存在"
    
    # 2. 校验艺术品
    artwork = get_artwork_by_id(artwork_id)
    if not artwork:
        return False, "艺术品不存在或已下架"
    
    # 3. 执行收藏
    success = add_collection(user_id, artwork_id)
    if success:
        return True, "已添加到收藏"
    else:
        return False, "该艺术品已在您的收藏中"

def remove_artwork_from_collection(user_id: int, artwork_id: int) -> tuple[bool, str]:
    """
    从收藏中移除艺术品
    返回：(是否成功, 提示信息)
    """
    # 1. 校验用户身份
    user = get_user_by_id(user_id)
    if not user:
        logger.warning(f"用户不存在（user_id: {user_id}）")
        return False, "用户不存在"
    
    # 2. 执行取消收藏
    success = remove_collection(user_id, artwork_id)
    if success:
        return True, "已从收藏中移除"
    else:
        return False, "该艺术品不在您的收藏中"

def get_user_collection_list(user_id: int) -> tuple[list, str]:
    """
    获取用户收藏列表
    返回：(收藏列表, 提示信息)
    """
    # 1. 校验用户身份
    user = get_user_by_id(user_id)
    if not user:
        logger.warning(f"用户不存在（user_id: {user_id}）")
        return [], "用户不存在"
    
    # 2. 获取收藏列表
    collections = get_user_collections(user_id)
    return collections, "获取收藏列表成功"